package com.mrd.gtimp.order.service;

import com.mrd.base.BaseBiz;
import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.exception.SaveException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.gtimp.business.entity.Declaration;
import com.mrd.gtimp.order.entity.DeclaContainer;
import com.mrd.gtimp.order.repository.DeclaContainerDao;
import com.mrd.spring.repository.BaseRepository;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * Created by lenovo on 2016/9/20.
 */

/**
 * 业务逻辑层 集装箱
 *
 * @author zyy
 */
@Service
public class DeclaContainerBiz extends BaseBiz<DeclaContainer> {

    @Resource
    // 该处指定注入BaseRepository的dao对象
    @Qualifier("declaContainerDao")
    public void setBaseRepository(BaseRepository<DeclaContainer, String> baseRepository) {
        super.baseRepository = baseRepository;
    }

    @Resource
    private DeclaContainerDao declaContainerDao;

    /**
     * 添加 集装箱
     */
    public Result containerAdd(DeclaContainer declaContainer) {
        try {
            if (declaContainer != null) {
                if (StringUtils.isNotEmpty(declaContainer.getId())) {
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("id", declaContainer.getId());
                    map.put("containerNo", declaContainer.getContainerNo());
                    map.put("containerStandard", declaContainer.getContainerStandard());
                    map.put("containerWeight", declaContainer.getContainerWeight());
                    String hql = "update com.mrd.gtimp.order.entity.DeclaContainer t" +
                            " set t.containerNo=:containerNo, t.containerStandard=:containerStandard" +
                            " t.containerWeight=:containerWeight where t.id=:id";
                    updateByHql(hql); //修改集装箱
                }
                saveOrupdateEntity(declaContainer); //添加
            }
            return Result.newSuccessInstance("添加集装箱成功");
        } catch (SaveException e) {
            e.printStackTrace();
        }
        return Result.newFailInstance(MsgConstants.SAVE_FAIL);
    }

    public List<DeclaContainer> findByDeclaration(Declaration declaration) {
        return declaContainerDao.findByDeclaration(declaration);
    }


    /**
     * 检查最近若干天柜是否在其他报关单中已被使用
     *
     * @param declarationId 单前报关单号
     * @param containerNoes 柜号清单
     * @return 数量
     */
    // TODO: 2016/11/5 需要调研需求再完善
    public int findLastSameContainerOrder(String declarationId, String[] containerNoes) {
        if (ArrayUtils.isEmpty(containerNoes)) {
            return 0;
        }
        StringBuilder sql = new StringBuilder();
        Map<String, Object> parameters = new HashMap<>();
        sql.append("select b.business_id,b.id from t_biz_decla_container c join t_biz_base_business b on c.declaration_id_ = b.id where 1=1 ");
        if (StringUtils.isNotEmpty(declarationId)) {
            sql.append("and b.id != :declarationId ");
            parameters.put("declarationId", declarationId);
        }
        sql.append("and b.ctime >= trunc((sysdate-:lastDays),'dd') and c.container_no_ in (");
        parameters.put("lastDays", 30);
        for (int i = 0; i < containerNoes.length; i++) {
            sql.append(":s").append(i).append(",");
            parameters.put("s" + i, containerNoes[i]);
        }
        sql.deleteCharAt(sql.length() - 1);
        sql.append(")");
        try {
            List<Object> objects = declaContainerDao.findEntityBySql(sql.toString(), -1, 10, parameters);
            return objects.size();
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
